/**
 * Created by Administrator on 2017/6/26.
 */
var vm_proList = avalon.define({
    $id: 'proList',
    menuList: [],
    proList: [],
    classify: [],
    place: [],
    unlockMode: [],
    colour:[],
    isseries:false,
    proName:'',
    category:'',
    usePlace:'',
    tnTab: function () {
        var self=this;
        $(".left-menu li >a").eq(0).addClass("active")
        $(".left-menu li >a").click(function () {
            currentIndex = $(this).parent().index();
            console.log("currentIndex=" + currentIndex)
            $(".left-menu li").eq(currentIndex).find(">a").addClass("active").parent().siblings().find(">a").removeClass("active")
            var hasClassIcon = $(this).find("span").hasClass("icon-rightTip");
            if (hasClassIcon) {
                $(this).find("span:last-child").removeClass("icon-rightTip").addClass("icon-downTip");
            } else {
                $(this).find("span:last-child").removeClass("icon-downTip").addClass("icon-rightTip");
            }
             //$(this).next().toggle().addClass("tnTab");
            self.isseries = !self.self;
        })
        $(".seriesList a").click(function () {
            console.log("currentIndex=" + currentIndex + ";;$(this).index==" + $(this).index());
            $(this).parent().parent().siblings().find(".seriesList a span").removeClass("red");
            $(this).find("span").addClass("red").parent().siblings().find("span").removeClass("red");
        })
    },
    refreshProList: function (name) {
        this.category = name;
        console.log('this.category =='+this.category);
        this.getProListData();
    },
    goContent: function (id) {
        console.log("id==" + id);
        window.location.href = "content.html?id=" + id;
    },
    getProListData:function () {
        console.log("click me......")
        var self = this;
        ff.util.submit({
            url: 'product/getProductList',
            data: {
                pageIndex:1,
                pageSize:10,
                name:self.proName,
                category:self.category,
                usePlace:self.usePlace,
                unlockType:''
            },
            success: function (res) {
                if (res.resultStatus == 100) { 
                    for(var i=0,len=res.resultData.product.length;i<len;i++){
                        res.resultData.product[i].colourName = formatColor(res.resultData.product[i].colour,1);
                        res.resultData.product[i].placeName = formatColor(res.resultData.product[i].usePlace,2);
                        res.resultData.product[i].unlockTypeName = formatColor(res.resultData.product[i].unlockType,3);
                    }
                    self.proList = [];
                    self.proList = res.resultData;
                    console.log(JSON.stringify(self.proList.product.length, null, 4));
                }
            }
        });
    }
})
getMenuData()
function getMenuData() {
    ff.util.submit({
        url: 'product/getPageData',
        data: {},
        success: function (res) {
            if (res.resultStatus == 100) {
                vm_proList.menuList = res.resultData.menu;
                vm_proList.classify =res.resultData.category;
                vm_proList.place = res.resultData.usePlace;
                vm_proList.unlockMode = res.resultData.unlockType;
                vm_proList.colour = res.resultData.colour;
                vm_proList.getProListData();
                var obj={
                    place:res.resultData.usePlace,
                    colour:res.resultData.colour,
                    unlockMode:res.resultData.unlockType
                }
                ff.cache.set("proAttributes",obj);
                console.log(ff.cache.get('proAttributes'))
                // console.log(JSON.stringify(res.resultData.menu, null, 4));
            }
        }
    });
}

function formatColor(id,style) {
    var idArr = id.split(",");
    var id_len = idArr.length;
    var colorArr
    if(style == 1){
        colorArr = vm_proList.colour;
    }else if(style == 2){
        colorArr = vm_proList.place;
    }else if(style == 3){
        colorArr = vm_proList.unlockMode;
    }
    var color=[];
    for(var j=0;j<id_len;j++){
        for(var i=0,len=colorArr.length;i<len;i++){
            if(idArr[j]==colorArr[i].id){
                color.push(colorArr[i].label);
            }
        }
    }
    console.log("color==="+color);
    return color;
}

function formatPlace(id) {
    var idArr = id.split(",");
    var id_len = idArr.length;
    var placeArr = vm_proList.place;
    var place=[];
    for(var j=0;j<id_len;j++){
        for(var i=0,len=placeArr.length;i<len;i++){
            if(idArr[j]==placeArr[i].id){
                place.push(placeArr[i].label);
            }
        }
    }
    console.log("place==="+place);
    return place;
}

function getProductData(id) {
   // getProData(id);
    $.ajax({
        url: stitching_info_url(id),
        dataType: 'json',
        success: function (data, status, xhr) {
            console.log("getProData===" + JSON.stringify(data, null, 4))
            vm_proList.proList = data.product;
        }
    })
}

var n = 0;
var currentIndex = 0;


